package com.ktgj.other.controller;

import com.alibaba.fastjson.JSONObject;
import com.ktgj.common.core.enums.EmHelper;
import com.ktgj.common.core.utils.xml.XElement;
import com.ktgj.customer.api.domain.BillPayment;
import com.ktgj.customer.api.domain.PaymentCallbackVo;
import com.ktgj.other.common.RentCar;
import com.ktgj.other.domain.*;
import com.ktgj.other.domain.common.*;
import com.ktgj.other.domain.vo.PhoneSoundCallback;
import com.ktgj.other.mapper.*;
import com.ktgj.other.service.*;
import com.ktgj.pay.api.RemotePayService;
import com.ktgj.pay.api.domain.UnionPayQRCodeResponseApi;
import com.ktgj.pay.api.domain.YeeBaoResultApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@ControllerAdvice
@RestController
@RequestMapping("/callback")
public class CallbackController {

    @Autowired
    private RemotePayService payService;
    @Autowired
    private EtOrderMapper etOrderMapper;
    @Autowired
    private EtOrderCyclerecordMapper etOrderCyclerecordMapper;
    @Autowired
    private EtOrderPaymentrecordMapper etOrderPaymentrecordMapper;
    @Autowired
    private EtOrderRefundrecordMapper etOrderRefundrecordMapper;
    @Autowired
    private IEtOrderService etOrderService;
    @Autowired
    private EtOrderTrackMapper etOrderTrackMapper;
    @Autowired
    private RemotePayService remotePayService;
    @Autowired
    private IRcOrderService rcOrderService;
    @Autowired
    private IRcOrderDetailService rcOrderDetailService;
    @Autowired
    private IRcOrderPaymentrecordService rcOrderPaymentrecordService;
    @Autowired
    private IRcOrderTrackingService rcOrderTrackingService;
    @Autowired
    private RentCar rentCar;

    /**
     * 微信小程序支付回调
     * @param resultMap
     * @param resultMap
     */
    @RequestMapping("/wechatPayCallback")
    public void wechatPayCallback(@RequestBody Map<String, String> resultMap) {
        boolean result = payService.isPaySuccessful(resultMap);
        String returnCode = resultMap.put("return_code", resultMap.get("return_code"));
        String resultCode = resultMap.put("result_code", resultMap.get("result_code"));
        String outTradeNo = resultMap.put("out_trade_no", resultMap.get("out_trade_no"));
        String transaction_id = resultMap.put("transaction_id", resultMap.get("transaction_id"));
        if (result) {
            if (returnCode.equals("SUCCESS") && resultCode.equals("SUCCESS")) {//交易成功
                if (outTradeNo != null) {//商户订单号
                    EtOrder etOrder = new EtOrder();
                    etOrder.setPaystatus(20l);
                    etOrder.setOrderstatus(10l);
                    etOrder.setOrderNum(outTradeNo);
                    int i = etOrderMapper.updateOrderStart(etOrder);
                    if (i <= 0){
                        System.out.println("修改订单状态失败");
                    }

                    etOrder = etOrderMapper.etOrder(outTradeNo);

                    EtOrderCyclerecord etOrderCyclerecord = new EtOrderCyclerecord();
                    etOrderCyclerecord.setOrderid(etOrder.getOrderid());
                    etOrderCyclerecord.setCyclerecordstatus(101l);
                    etOrderCyclerecord.setCreateTime(new Date());
                    etOrderCyclerecord.setRemark("订单支付成功，等待确认订单已支付成功");
                    i = etOrderCyclerecordMapper.insertEtOrderCyclerecord(etOrderCyclerecord);
                    if (i <= 0){
                        System.out.println("添加订单支付日志信息失败");
                    }

                    EtOrderPaymentrecord etOrderPaymentrecord = new EtOrderPaymentrecord();
                    etOrderCyclerecord.setOrderid(etOrder.getOrderid());
                    etOrderPaymentrecord.setTransactionnumber(transaction_id);
                    etOrderPaymentrecord.setPaymenttype(EmHelper.cardType.Applets.getValue());
                    etOrderPaymentrecord.setPaymentstatus(EmHelper.OrderStatus.Paid.getValue());
                    etOrderPaymentrecord.setRemark("门票订单-订单号" + etOrder.getOrderid() + ",支付金额-" + etOrder.getAmountprice());
                    etOrderPaymentrecord.setCreattime(new Date());
                    etOrderPaymentrecord.setUpdateTime(new Date());
                    i = etOrderPaymentrecordMapper.insertEtOrderPaymentrecord(etOrderPaymentrecord);
                    if (i <= 0){
                        System.out.println("添加支付信息说明失败");
                    }

                    HttpTongChengInfoOrder httpTongChengInfoOrder = null;
                    try {
                        httpTongChengInfoOrder = etOrderService.orderPlace(etOrder.getOrderNum());
                    } catch (Exception e){
                        System.out.println(e.getMessage());
                    }

                    if (httpTongChengInfoOrder != null){
                        if (httpTongChengInfoOrder.getRespCode() != 1){
                            System.out.println(httpTongChengInfoOrder.getRespMsg());
                        }
                        else {
                            etOrder.setPartnerordernumber(httpTongChengInfoOrder.getSerialId());
                            etOrderMapper.updateEtOrder(etOrder);
                        }
                        System.out.println("支付成功");
                    }
                    else {
                        System.out.println("验证失败");
                    }
                }
                else {
                    System.out.println("商户订单号为空");
                }
            }
            else {
                if (outTradeNo != null) {
                    EtOrder etOrder = new EtOrder();
                    etOrder.setPaystatus(20l);
                    etOrder.setOrderNum(outTradeNo);
                    int i = etOrderMapper.updateOrderStart(etOrder);
                    if (i <= 0){
                        System.out.println("修改订单状态失败");
                    }

                    EtOrderCyclerecord etOrderCyclerecord = new EtOrderCyclerecord();
                    etOrderCyclerecord.setOrderid(etOrder.getOrderid());
                    etOrderCyclerecord.setCyclerecordstatus(0l);
                    etOrderCyclerecord.setCreateTime(new Date());
                    etOrderCyclerecord.setRemark("支付失败");
                    i = etOrderCyclerecordMapper.insertEtOrderCyclerecord(etOrderCyclerecord);
                    if (i <= 0){
                        System.out.println("添加订单支付日志信息失败");
                    }

                    EtOrderPaymentrecord etOrderPaymentrecord = new EtOrderPaymentrecord();
                    etOrderCyclerecord.setOrderid(etOrder.getOrderid());
                    etOrderPaymentrecord.setTransactionnumber(transaction_id);
                    etOrderPaymentrecord.setPaymenttype(EmHelper.cardType.Applets.getValue());
                    etOrderPaymentrecord.setPaymentstatus(EmHelper.OrderStatus.Paid.getValue());
                    etOrderPaymentrecord.setRemark("支付失败");
                    etOrderPaymentrecord.setCreattime(new Date());
                    etOrderPaymentrecord.setUpdateTime(new Date());
                    i = etOrderPaymentrecordMapper.insertEtOrderPaymentrecord(etOrderPaymentrecord);
                    if (i <= 0){
                        System.out.println("添加支付信息说明失败");
                    }

                    System.out.println("支付失败");
                }
                else {
                    System.out.println("商户订单号为空");
                }
            }
        }
    }

    /**
     * 银联二维码支付回调
     * */
    @PostMapping("/otherUnionQRCodePayCallback")
    public void otherUnionQRCodePayCallback(HttpServletResponse response, HttpServletRequest request){
        PaymentCallbackVo paymentCallbackVo = new PaymentCallbackVo();
        String parameter = request.getParameter("billPayment");
        BillPayment parseObject = JSONObject.parseObject(parameter, BillPayment.class);
        paymentCallbackVo.setBillPayment(parseObject);
        paymentCallbackVo.setMid(request.getParameter("mid"));//商户号
        paymentCallbackVo.setTid(request.getParameter("tid"));//终端号
        paymentCallbackVo.setInstMid(request.getParameter("instMid"));//业务类型
        paymentCallbackVo.setBillNo(request.getParameter("billNo"));//账单号
        paymentCallbackVo.setBillQRCode(request.getParameter("billQRCode"));//账单二维码
        paymentCallbackVo.setBillStatus(request.getParameter("billStatus"));//账单状态
        paymentCallbackVo.setBillDesc(request.getParameter("billDesc"));//账单描述
        paymentCallbackVo.setMemberId(request.getParameter("memberId"));//会员号
        paymentCallbackVo.setCounterNo(request.getParameter("counterNo"));//桌号、柜台号、房间号
        paymentCallbackVo.setMerName(request.getParameter("merName"));//商户名称
        paymentCallbackVo.setMemo(request.getParameter("memo"));//付款附言
        paymentCallbackVo.setNotifyId(request.getParameter("notifyId"));//支付通知ID
        paymentCallbackVo.setSecureStatus(request.getParameter("secureStatus"));//担保状态
        paymentCallbackVo.setCompleteAmount(request.getParameter("completeAmount"));//担保完成金额（分）
        paymentCallbackVo.setBillDate(request.getParameter("billDate"));//订单时间，格式yyyy-MM-dd
        paymentCallbackVo.setCreateTime(request.getParameter("createTime"));//账单创建时间，格式yyyy-MM-dd HH:mm:ss
        paymentCallbackVo.setSign(request.getParameter("sign"));//签名
        paymentCallbackVo.setExtraBuyerInfo(request.getParameter("extraBuyerInfo"));//用户额外信息

        EtOrder etOrder = etOrderMapper.etOrder(paymentCallbackVo.getBillNo().substring(4));

        if (paymentCallbackVo.getBillStatus().equals("PAID")) {

            etOrder.setPaystatus(20l);
            int i = etOrderMapper.updateEtOrder(etOrder);
            if (i <= 0){
                System.out.println("订单支付状态修改失败");
            }

            EtOrderCyclerecord etOrderCyclerecord = new EtOrderCyclerecord();
            etOrderCyclerecord.setOrderid(etOrder.getOrderid());
            etOrderCyclerecord.setCyclerecordstatus(101l);
            etOrderCyclerecord.setCreateTime(new Date());
            etOrderCyclerecord.setRemark("订单支付成功");
            i = etOrderCyclerecordMapper.insertEtOrderCyclerecord(etOrderCyclerecord);
            if (i <= 0){
                System.out.println("添加订单支付记录失败");
            }

            HttpTongChengInfoOrder httpTongChengInfoOrder = null;
            try {
                httpTongChengInfoOrder = etOrderService.orderPlace(etOrder.getOrderNum());
            } catch (Exception e){
                System.out.println("验证失败");
            }

            if (httpTongChengInfoOrder != null){
                if (httpTongChengInfoOrder.getRespCode() != 1){
                    System.out.println(httpTongChengInfoOrder.getRespMsg());
                }
                else {
                    etOrder.setPartnerordernumber(httpTongChengInfoOrder.getSerialId());
                    etOrderMapper.updateEtOrder(etOrder);
                }
            }
            else {
                System.out.println("验证失败");
            }

        } else {
            etOrder.setPaystatus(30l);
            int i = etOrderMapper.updateEtOrder(etOrder);
            if (i <= 0){
                System.out.println("订单支付状态修改失败");
            }

            EtOrderCyclerecord etOrderCyclerecord = new EtOrderCyclerecord();
            etOrderCyclerecord.setOrderid(etOrder.getOrderid());
            etOrderCyclerecord.setCyclerecordstatus(0l);
            etOrderCyclerecord.setCreateTime(new Date());
            etOrderCyclerecord.setRemark("订单支付失败:" + paymentCallbackVo.getBillDesc());
            i = etOrderCyclerecordMapper.insertEtOrderCyclerecord(etOrderCyclerecord);
            if (i <= 0){
                System.out.println("添加订单支付记录失败");
            }
        }

        PrintWriter printWriter;
        response.setCharacterEncoding("UTF-8");
        try {
            printWriter = response.getWriter();
            printWriter.write("SUCCESS");
            printWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 语音支付回调
     * */
    @RequestMapping("/hotelVoicePayCallback")
    public void hotelVoicePayCallback(HttpServletResponse response, HttpServletRequest request){
        PhoneSoundCallback phoneSoundCallback = new PhoneSoundCallback();
        try
        {
            request.setCharacterEncoding("UTF-8");
//            String response_xml = Base64Decoder.decodeStr(request.getParameter("response_text"));
//            XElement root = XElement.loadXml(response_xml);
            XElement root = null;
            phoneSoundCallback.setAccountType(root.element("AccountType").getValue());
            phoneSoundCallback.setAcqSsn(root.element("AcqSsn").getValue());
            phoneSoundCallback.setAmount(root.element("Amount").getValue());
            phoneSoundCallback.setAccountNo(root.element("AccountNo").getValue());
            phoneSoundCallback.setAsynAddress(root.element("AsynAddress").getValue());
            phoneSoundCallback.setCurrency(root.element("Currency").getValue());
            phoneSoundCallback.setDescription(root.element("Description").getValue());
            phoneSoundCallback.setMAC(root.element("MAC").getValue());
            phoneSoundCallback.setMerchantNo(root.element("MerchantNo").getValue());
            phoneSoundCallback.setMerchantOrderNo(root.element("MerchantOrderNo").getValue());
            phoneSoundCallback.setMethod(root.element("Method").getValue());
            phoneSoundCallback.setOrderNo(root.element("OrderNo").getValue());
            phoneSoundCallback.setOrderState(root.element("OrderState").getValue());
            phoneSoundCallback.setOrderType(root.element("OrderType").getValue());
            phoneSoundCallback.setProcCode(root.element("ProcCode").getValue());
            phoneSoundCallback.setProcessCode(root.element("ProcessCode").getValue());
            phoneSoundCallback.setRemark(root.element("Remark").getValue());
            phoneSoundCallback.setRespCode(root.element("RespCode").getValue());
            phoneSoundCallback.setSettleDate(root.element("SettleDate").getValue());
            phoneSoundCallback.setTerminalNo(root.element("TerminalNo").getValue());
            phoneSoundCallback.setTsNo(root.element("TsNo").getValue());
            phoneSoundCallback.setUpsNo(root.element("UpsNo").getValue());
            phoneSoundCallback.setUserPayedTime(root.element("UserPayedTime").getValue());
            phoneSoundCallback.setVersion(root.element("Version").getValue());
        } catch (UnsupportedEncodingException e)
        {
            e.printStackTrace();
        }

        if (phoneSoundCallback.getRespCode().equals("0000")){//交易成功
            if (phoneSoundCallback.getMerchantOrderNo() != null){
                EtOrder etOrder = etOrderMapper.etOrder(phoneSoundCallback.getMerchantOrderNo());

                etOrder.setPaystatus(20l);
                int i = etOrderMapper.updateEtOrder(etOrder);
                if (i <= 0){
                    System.out.println("订单支付状态修改失败");
                }

                EtOrderCyclerecord etOrderCyclerecord = new EtOrderCyclerecord();
                etOrderCyclerecord.setOrderid(etOrder.getOrderid());
                etOrderCyclerecord.setCyclerecordstatus(101l);
                etOrderCyclerecord.setCreateTime(new Date());
                etOrderCyclerecord.setRemark("订单支付成功");
                i = etOrderCyclerecordMapper.insertEtOrderCyclerecord(etOrderCyclerecord);
                if (i <= 0){
                    System.out.println("添加订单支付记录失败");
                }

                HttpTongChengInfoOrder httpTongChengInfoOrder = null;
                try {
                    httpTongChengInfoOrder = etOrderService.orderPlace(etOrder.getOrderNum());
                } catch (Exception e){
                    System.out.println("验证失败");
                }

                if (httpTongChengInfoOrder != null){
                    if (httpTongChengInfoOrder.getRespCode() != 1){
                        System.out.println(httpTongChengInfoOrder.getRespMsg());
                    }
                    else {
                        etOrder.setPartnerordernumber(httpTongChengInfoOrder.getSerialId());
                        etOrderMapper.updateEtOrder(etOrder);
                    }
                }
                else {
                    System.out.println("验证失败");
                }
            }
        }
        else {
            if (phoneSoundCallback.getMerchantOrderNo() != null){
                EtOrder etOrder = etOrderMapper.etOrder(phoneSoundCallback.getMerchantOrderNo());

                etOrder.setPaystatus(30l);
                int i = etOrderMapper.updateEtOrder(etOrder);
                if (i <= 0){
                    System.out.println("订单支付状态修改失败");
                }

                EtOrderCyclerecord etOrderCyclerecord = new EtOrderCyclerecord();
                etOrderCyclerecord.setOrderid(etOrder.getOrderid());
                etOrderCyclerecord.setCyclerecordstatus(0l);
                etOrderCyclerecord.setCreateTime(new Date());
                etOrderCyclerecord.setRemark("订单支付失败");
                i = etOrderCyclerecordMapper.insertEtOrderCyclerecord(etOrderCyclerecord);
                if (i <= 0){
                    System.out.println("添加订单支付记录失败");
                }
            }
        }
    }

    /**
     * 退款结果回调接口
     * */
    @PostMapping("/refundResultCallback")
    public HttpTongChengInfoCallback refundResultCallback(TongChengInfoCallback tongChengInfoCallback){
        if (tongChengInfoCallback.getReturnCode().equals("1")){
            EtOrder etOrder = new EtOrder();
            etOrder.setPartnerordernumber(tongChengInfoCallback.getSerialId());
            etOrder.setOrderBillId(String.valueOf(tongChengInfoCallback.getOrderBillId()));
            etOrder.setOrderstatus(40l);
            int i = etOrderMapper.updateOrderBillId(etOrder);
            if (i <= 0){
                System.out.println("添加订单退款流水号失败");
            }

            EtOrder order = etOrderMapper.order(tongChengInfoCallback.getSerialId());

            EtOrderRefundrecord etOrderRefundrecord = new EtOrderRefundrecord();
            etOrderRefundrecord.setOrderid(order.getOrderid());
            etOrderRefundrecord.setRefundamount(new BigDecimal(tongChengInfoCallback.getRefundAmount()));
            etOrderRefundrecord.setPoundageamount(new BigDecimal(tongChengInfoCallback.getPoundageAmount()));
            // todo 金色实际退款手续费
            etOrderRefundrecord.setJsjpoundageamount(new BigDecimal(0));
            etOrderRefundrecord.setProcesstime(new Date());
            etOrderRefundrecord.setIsdispose(1l);
            i = etOrderRefundrecordMapper.updateOrderRefundrecord(etOrderRefundrecord);
            if (i <= 0){
                System.out.println("补全退款信息失败");
            }

            EtOrderTrack etOrderTrack = new EtOrderTrack();
            etOrderTrack.setOrderid(order.getOrderid());
            etOrderTrack.setTitle("金色世纪处理");
            etOrderTrack.setContents("金色世纪将在一个工作日内完成审核，请耐心等待");
            etOrderTrack.setCreateTime(new Date());
            i = etOrderTrackMapper.insertEtOrderTrack(etOrderTrack);
            if (i <= 0){
                System.out.println("添加退款流程信息失败");
            }

            //退款
            EtOrderPaymentrecord etOrderPaymentrecord = etOrderPaymentrecordMapper.orderPaymentrecord(etOrder.getOrderid());
            //微信小程序
            if (etOrderPaymentrecord.getPaymenttype().equals(EmHelper.cardType.Applets.getValue())){

            }
            //信用卡
            else if (etOrderPaymentrecord.getPaymenttype().equals(EmHelper.cardType.CREDIT_CARD.getValue())){
                Map<String, String> map = new HashMap<>();
                map.put("jsjOrderId", etOrder.getOrderid().toString());
                map.put("bankTrxId", etOrderPaymentrecord.getTransactionnumber());

                BigDecimal refundAmount = new BigDecimal(tongChengInfoCallback.getRefundAmount());
                BigDecimal jsjpoundageamount = new BigDecimal(0);
                BigDecimal moneyAmout = refundAmount.subtract(jsjpoundageamount);
                map.put("moneyAmout", moneyAmout.toString());

                if (etOrder.getSourceway() == 30){
                    map.put("payType", "App");
                }
                else if (etOrder.getSourceway() == 40){
                    map.put("payType", "Crm");
                }

                YeeBaoResultApi yeeBaoResultApi = remotePayService.bankCreditRefund(map);
                System.out.println(yeeBaoResultApi.getCode() + yeeBaoResultApi.getErrorMsg());
            }
            //借记卡
            else if (etOrderPaymentrecord.getPaymenttype().equals(EmHelper.cardType.DEBIT_CARD.getValue())){
                Map<String, String> map = new HashMap<>();
                map.put("jsjOrderId", etOrder.getOrderid().toString());
                map.put("bankTrxId", etOrderPaymentrecord.getTransactionnumber());

                BigDecimal refundAmount = new BigDecimal(tongChengInfoCallback.getRefundAmount());
                BigDecimal jsjpoundageamount = new BigDecimal(0);
                BigDecimal moneyAmout = refundAmount.subtract(jsjpoundageamount);
                map.put("moneyAmout", moneyAmout.toString());

                YeeBaoResultApi yeeBaoResultApi = remotePayService.bankDebitRefund(map);
                System.out.println(yeeBaoResultApi.getCode() + yeeBaoResultApi.getErrorMsg());
            }
            //银联二维码
            else if (etOrderPaymentrecord.getPaymenttype().equals(EmHelper.cardType.UnionPayQR.getValue())){
                Map<String,String> map = new HashMap<>();

                BigDecimal refundAmount = new BigDecimal(tongChengInfoCallback.getRefundAmount());
                BigDecimal jsjpoundageamount = new BigDecimal(0);
                BigDecimal moneyAmout = refundAmount.subtract(jsjpoundageamount);
                map.put("refundAmount", moneyAmout.toString());

                String billNo = "5676" + etOrder.getOrderid();
                map.put("billNo", billNo);
                map.put("notifyUrl", "http://106.38.39.137:8080/other/callback/unionQRCodePayCallback");
                UnionPayQRCodeResponseApi unionPayQRCodeResponseApi = remotePayService.qrCodeRefund(map);
                System.out.println(unionPayQRCodeResponseApi.getErrCode() + unionPayQRCodeResponseApi.getErrMsg());
            }
            //语音
            else if (etOrderPaymentrecord.getPaymenttype().equals(EmHelper.cardType.Voice.getValue())){

            }
        }
        else {
            EtOrder order = etOrderMapper.order(tongChengInfoCallback.getSerialId());

            EtOrderRefundrecord etOrderRefundrecord = new EtOrderRefundrecord();
            etOrderRefundrecord.setOrderid(order.getOrderid());
            if (tongChengInfoCallback.getRefundAmount() != null && !tongChengInfoCallback.getRefundAmount().equals("")){
                etOrderRefundrecord.setRefundamount(new BigDecimal(tongChengInfoCallback.getRefundAmount()));
            }
            if (tongChengInfoCallback.getPoundageAmount() != null && !tongChengInfoCallback.getPoundageAmount().equals("")){
                etOrderRefundrecord.setPoundageamount(new BigDecimal(tongChengInfoCallback.getPoundageAmount()));
            }

            // todo 金色实际退款手续费
            etOrderRefundrecord.setJsjpoundageamount(new BigDecimal(0));
            etOrderRefundrecord.setRefundstatus(34l);
            etOrderRefundrecord.setProcesstime(new Date());
            etOrderRefundrecord.setIsdispose(0l);
            int i = etOrderRefundrecordMapper.updateOrderRefundrecord(etOrderRefundrecord);
            if (i <= 0){
                System.out.println("补全退款信息失败");
            }

            EtOrderTrack etOrderTrack = new EtOrderTrack();
            etOrderTrack.setOrderid(order.getOrderid());
            etOrderTrack.setTitle("退款异常");
            etOrderTrack.setContents(tongChengInfoCallback.getReturnMsg());
            etOrderTrack.setCreateTime(new Date());
            i = etOrderTrackMapper.insertEtOrderTrack(etOrderTrack);
            if (i <= 0){
                System.out.println("添加退款流程信息失败");
            }
        }

        HttpTongChengInfoCallback httpTongChengInfoCallback = new HttpTongChengInfoCallback();
        httpTongChengInfoCallback.setRespCode(1);
        httpTongChengInfoCallback.setRespMsg("回调数据更新成功");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        httpTongChengInfoCallback.setRespTime(format.format(new Date()));
        return httpTongChengInfoCallback;
    }

    /**
     * 银联二维码退款回调
     */
    @PostMapping("/unionQRCodePayCallback")
    public void unionQRCodePayCallback(HttpServletResponse response, HttpServletRequest request) {
        String billNo = request.getParameter("billNo"); //账单号
        String refundStatus = request.getParameter("refundStatus"); //退款结果（SUCCESS成功 FAIL失败 PROCESSING处理）
        if (refundStatus.equals("SUCCESS")) {
            String orderId = billNo.substring(4);
            EtOrder etOrder = etOrderMapper.selectEtOrderById(Long.valueOf(orderId));
            EtOrderRefundrecord etOrderRefundrecord = new EtOrderRefundrecord();
            etOrderRefundrecord.setOrderid(etOrder.getOrderid());
            etOrderRefundrecord.setRefundstatus(33l);
            etOrderRefundrecord.setIsdispose(1l);
            int i = etOrderRefundrecordMapper.updateOrderRefundrecord(etOrderRefundrecord);
            if (i <= 0){
                System.out.println("修改支付信息状态失败");
            }

            EtOrderTrack etOrderTrack = new EtOrderTrack();
            etOrderTrack.setOrderid(etOrder.getOrderid());
            etOrderTrack.setTitle("退款成功");
            etOrderTrack.setContents("退款已完成");
            i = etOrderTrackMapper.insertEtOrderTrack(etOrderTrack);
            if (i <= 0){
                System.out.println("添加退款流程失败");
            }

            PrintWriter printWriter;
            response.setCharacterEncoding("UTF-8");
            try {
                printWriter = response.getWriter();
                printWriter.write("success");
                printWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else if (refundStatus.equals("FAIL")) {

            String orderId = billNo.substring(4);
            EtOrder etOrder = etOrderMapper.selectEtOrderById(Long.valueOf(orderId));
            EtOrderRefundrecord etOrderRefundrecord = new EtOrderRefundrecord();
            etOrderRefundrecord.setOrderid(etOrder.getOrderid());
            etOrderRefundrecord.setRefundstatus(34l);
            etOrderRefundrecord.setIsdispose(0l);
            int i = etOrderRefundrecordMapper.updateOrderRefundrecord(etOrderRefundrecord);
            if (i <= 0){
                System.out.println("修改支付信息状态失败");
            }

            EtOrderTrack etOrderTrack = new EtOrderTrack();
            etOrderTrack.setOrderid(etOrder.getOrderid());
            etOrderTrack.setTitle("退款失败");
            etOrderTrack.setContents("未完成退款");
            i = etOrderTrackMapper.insertEtOrderTrack(etOrderTrack);
            if (i <= 0){
                System.out.println("添加退款流程失败");
            }

            PrintWriter printWriter;
            response.setCharacterEncoding("UTF-8");
            try {
                printWriter = response.getWriter();
                printWriter.write("SUCCESS");
                printWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 专车异步订单回调
     * @param response
     * @param request
     */
    @RequestMapping("/rentCarOrderCallback")
    public void asynchronousOrder(HttpServletResponse response, @RequestBody OrderStatuCallBackRequest request) {
        RcOrder rcOrder = rcOrderService.queryRcOrderByOrderid(Long.valueOf(request.vendorOrderCode));
        if (rcOrder != null) {
            RcOrderTracking orderTracking = new RcOrderTracking();
            if (request.status.equals("dispatched")) {
                rcOrder.setOrderstatus(3L);
                rcOrderService.updateRcOrder(rcOrder);
                OrderDetailRequest detailRequest = new OrderDetailRequest();
                try {
                    detailRequest.orderCode = request.orderCode;
                    detailRequest.vendorOrderCode = request.vendorOrderCode;
                    OrderDetailResponse orderDetail = rentCar.orderDetail(detailRequest);
                    if (orderDetail.code == 1) {
                        if (orderDetail.driver != null) {
                            RcOrderDetail rcOrderDetail = rcOrderDetailService.selectRcOrderDetailById(rcOrder.getRcorderid());
                            rcOrderDetail.setCartype(orderDetail.driver.carName);
                            rcOrderDetail.setDrivername(orderDetail.driver.name);
                            rcOrderDetail.setDrivertel(orderDetail.driver.mobile);
                            rcOrderDetail.setPlatenumbers(orderDetail.driver.carNumber);
                            rcOrderDetailService.updateRcOrderDetail(rcOrderDetail);
                            orderTracking.setOrderid(Long.valueOf(rcOrder.getOrderid()));
                            orderTracking.setTrackingnode("确认司机");
                            orderTracking.setTrackingcontent("您的用车司机" + orderDetail.driver.name + "已接单，电话：" + orderDetail.driver.mobile + "，车型：" + orderDetail.driver.carName + "，" + orderDetail.driver.carNumber + "，司机会在" + rcOrder.getGetonaddress() + "等您。");
                            orderTracking.setOpemployeeid(0L);
                            orderTracking.setCreateTime(new Date());
                            orderTracking.setUpdateTime(new Date());
                            orderTracking.setRemark("系统消息");
                            rcOrderTrackingService.insertRcOrderTracking(orderTracking);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else if (request.status.equals("arrived")) {
                orderTracking.setOrderid(Long.valueOf(rcOrder.getOrderid()));
                orderTracking.setTrackingnode("确认司机");
                orderTracking.setTrackingcontent("司机已到达用户上车地点，等候您上车。");
                orderTracking.setOpemployeeid(0L);
                orderTracking.setCreateTime(new Date());
                orderTracking.setUpdateTime(new Date());
                orderTracking.setRemark("系统消息");
                rcOrderTrackingService.insertRcOrderTracking(orderTracking);
            } else if (request.status.equals("serviceStarted")) {
                rcOrder.setOrderstatus(4L);
                rcOrderService.updateRcOrder(rcOrder);
                orderTracking.setOrderid(Long.valueOf(rcOrder.getOrderid()));
                orderTracking.setTrackingnode("开始服务");
                orderTracking.setTrackingcontent("用车服务开始啦！");
                orderTracking.setOpemployeeid(0L);
                orderTracking.setCreateTime(new Date());
                orderTracking.setUpdateTime(new Date());
                orderTracking.setRemark("系统消息");
                rcOrderTrackingService.insertRcOrderTracking(orderTracking);
                rcOrder.setOrderstatus(4L);
                rcOrderService.updateRcOrder(rcOrder);
            } else if (request.status.equals("serviceFinished")) {
                rcOrder.setOrderstatus(5L);
                rcOrderService.updateRcOrder(rcOrder);
                orderTracking.setOrderid(Long.valueOf(rcOrder.getOrderid()));
                orderTracking.setTrackingnode("服务结束");
                orderTracking.setTrackingcontent("您已完成本次行程，期待下次为您服务。如您对本次行程有任何意见反馈，欢迎您点击这里提交，管家会积极改进。");
                orderTracking.setOpemployeeid(0L);
                orderTracking.setCreateTime(new Date());
                orderTracking.setUpdateTime(new Date());
                orderTracking.setRemark("系统消息");
                rcOrderTrackingService.insertRcOrderTracking(orderTracking);
                rcOrder.setOrderstatus(5L);
                rcOrderService.updateRcOrder(rcOrder);
            } else if (request.status.equals("canceled")) {
                rcOrder.setOrderstatus(6L);
                rcOrderService.updateRcOrder(rcOrder);
                orderTracking.setOrderid(Long.valueOf(rcOrder.getOrderid()));
                orderTracking.setTrackingnode("取消订单");
                orderTracking.setTrackingcontent("很抱歉，由于无司机接单，本次用车服务已取消，支付费用" + rcOrder.getPaymentamount() +"元会尽快为您退回，您可稍后再试。");
                orderTracking.setOpemployeeid(0L);
                orderTracking.setCreateTime(new Date());
                orderTracking.setUpdateTime(new Date());
                orderTracking.setRemark("系统消息");
                rcOrderTrackingService.insertRcOrderTracking(orderTracking);
                rcOrder.setOrderstatus(6L);
                rcOrderService.updateRcOrder(rcOrder);
            }
        }

        PrintWriter printWriter;
        response.setCharacterEncoding("UTF-8");
        try {
            printWriter = response.getWriter();
            printWriter.write("SUCCESS");
            printWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
